#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int longestArithSeqLength(vector<int>& nums) {
        int n = nums.size(), ret = 2;
        unordered_map<int, int> hash;
        hash[nums[0]] = 0;
        vector<vector<int>> dp(n, vector<int>(n, 2));
        for (int i = 1; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int a = nums[i] - (nums[j] - nums[i]);
                if (hash.count(a)) dp[j][i] = dp[i][hash[a]] + 1;
                ret = max(ret, dp[j][i]);
            }
            hash[nums[i]] = i;
        }
        return ret;
    }
};